package com.trxy.hotelsystem.contorller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.consumptionRecord;
import com.trxy.hotelsystem.service.impl.Consumption_recordServiceImpl;

@Controller
public class Consumption_recordController {
	
	@Autowired
	private Consumption_recordServiceImpl Consumption_recordServiceimpl;
	
	//会员查询自己的消费记录
	@RequestMapping(value = "selectConsumption_recordByvipNumber.do")
	public String selectConsumption_recordByvipNumber(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			Model model,consumptionRecord consumptionrecord,
			clientInf clientinf,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		clientInf us = (clientInf) session.getAttribute("viplogin");
		
		List<consumptionRecord> list = Consumption_recordServiceimpl.selectConsumption_recordByvipNumber(us.getNumber());
		
		for(consumptionRecord one:list) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String dateString = formatter.format(one.getTime());
			 one.setReserved2(dateString);
		}
		
		PageInfo<consumptionRecord> page = new PageInfo<>(list);
			
	    model.addAttribute("pageInfo", page);
		
		return "view/vipview/expenseRecord";
	}
	
	//确认订单送达
	@RequestMapping(value = "affirmIndent.do")
	public void affirmIndent(HttpServletRequest request, HttpServletResponse response,
			consumptionRecord consumptionrecord) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String serialnum = request.getParameter("serialnum");
		
		consumptionrecord.setSerialnum(serialnum);
		consumptionrecord.setState("1");
		
		int i = Consumption_recordServiceimpl.updaConsumption_recordByserialnum(consumptionrecord);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
		
	}
	
	//查询所有消费订单
	@RequestMapping(value = "queryConsumption_recordAll.do")
	public String queryConsumption_recordAll(HttpServletRequest request, HttpServletResponse response,
			Model model,consumptionRecord consumptionrecord,
			clientInf clientinf,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<consumptionRecord> list = Consumption_recordServiceimpl.queryConsumption_recordAll();
		
		for(consumptionRecord one:list) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String dateString = formatter.format(one.getTime());
			 one.setReserved2(dateString);
		}
		
		PageInfo<consumptionRecord> page = new PageInfo<>(list);
		
	    model.addAttribute("pageInfo", page);
		
		
		return "view/consumption_record/consumption_recordMain";
	}
	
	//多条件查询消费记录
	@RequestMapping(value = "selectConsump_Record.do")
	public String selectConsump_Record(HttpServletRequest request, HttpServletResponse response,
			Model model,consumptionRecord consumptionrecord,
			clientInf clientinf,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		String roomNumber = request.getParameter("roomNumber");
		String vipNumber = request.getParameter("vipNumber");
		String state = request.getParameter("state");
		
		if(roomNumber != "") {
			consumptionrecord.setRoomnumber(Integer.parseInt(roomNumber));
		}else {
			consumptionrecord.setRoomnumber(null);
		}
		if(vipNumber != "") {
			consumptionrecord.setReserved1(vipNumber);
		}else {
			consumptionrecord.setReserved1(null);
		}
		if(state != "") {
			if(state.equals("默认")) {
				consumptionrecord.setState(null);
			}else if(state.equals("未配送")) {
				consumptionrecord.setState("0");
			}else if(state.equals("配送中")) {
				consumptionrecord.setState("2");
			}else {
				consumptionrecord.setState("1");
			}
		}else {
			consumptionrecord.setState(null);
		}
		
		List<consumptionRecord> list = Consumption_recordServiceimpl.selectConsump_Record(consumptionrecord);
		
		for(consumptionRecord one:list) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String dateString = formatter.format(one.getTime());
			 one.setReserved2(dateString);
		}
		
		PageInfo<consumptionRecord> page = new PageInfo<>(list);

		model.addAttribute("pageInfo", page);
		model.addAttribute("roomNumber", roomNumber);
		model.addAttribute("vipNumber", vipNumber);
		model.addAttribute("state", state);
		model.addAttribute("inf", "多条件");
		
		
		return "view/consumption_record/consumption_recordMain";
	}
	
	//配送
	@RequestMapping(value = "delivery.do")
	public void delivery(HttpServletRequest request, HttpServletResponse response,
			consumptionRecord consumptionrecord) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String serialnum = request.getParameter("serialnum");
		
		consumptionrecord.setSerialnum(serialnum);
		consumptionrecord.setState("2");
		
		int i = Consumption_recordServiceimpl.updaConsumption_recordByserialnum(consumptionrecord);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
		
		
	}

}
